package cn.miao.hrm.controller;

import cn.miao.hrm.domain.TenantType;
import cn.miao.hrm.query.TenantTypeQuery;
import cn.miao.hrm.service.ITenantTypeService;
import cn.miao.util.AjaxResult;
import cn.miao.util.PageList;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/tenantType")
public class TenantTypeController {
    @Autowired
    public ITenantTypeService tenantTypeService;

    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody TenantType tenantType){
        if(tenantType.getId()!=null){
            tenantTypeService.updateById(tenantType);
        }else{
            tenantTypeService.insert(tenantType);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        tenantTypeService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(tenantTypeService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(tenantTypeService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody TenantTypeQuery query){
        Page<TenantType> page = new Page<>(query.getPage(), query.getRows());
        //增加条件
        /*SELECT id AS id,`name`,description
            FROM t_tenant_type
                WHERE (name LIKE '%培训%' OR description LIKE '%培训%')
        LIMIT 0,10;
        */
        Wrapper<TenantType> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getKeyword()), "name", query.getKeyword(), SqlLike.DEFAULT);
        wrapper.or().like(StringUtils.isNotBlank(query.getKeyword()), "description", query.getKeyword(),SqlLike.DEFAULT);
        page = tenantTypeService.selectPage(page, wrapper);

        PageList<TenantType> pageList = new PageList<>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
